<style scoped>
.goods-item {
    box-shadow: 0 0 2px rgba(0,0,0,0.2);
    margin-bottom: 6px;
    text-align: center;
}
.goods-item img{
    width: 100%;
}
.goods-name {
    color: #666;
    font-size: 12px;
    height: 24px;
    line-height: 24px;
    margin-top: 4px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    line-clamp: 1;
    -webkit-box-orient: vertical;
}
.goods-price-wrap {
    font-size: 14px;
    color: #000;
    padding: 6px;
    display: flex;
    justify-content: space-between;
}
.goods-price-wrap .sell-count {
    font-size: 10px;
    color: #666;
}
.goods-price-wrap .goods-price {
    font-weight: bold;
}
.goods-price-wrap .goods-price .sc-price {
    color: #666;
    text-decoration: line-through;
    font-size: 9px;
    margin-left: 5px;
}
.goods-price-wrap .goods-price .dp-price {
    color: red;
    font-size: 12px;
}

.goods-price-wrap .goods-item-icon-cart {
    font-size: 14px;
    border-radius: 50%;
    width: 0.5rem;
    height: 0.5rem;
    color: red;
    display: flex;
    justify-content: center;
    align-items: center;
}
.goods-price-wrap .goods-item-icon-cart .icon-cart-loading {
    font-size: 16px;
}

.goods-image-wrap {
    position: relative;
    width: 100%;
    padding-top: 100%;
    overflow: hidden;
    background-color: #fff;
}
.goods-image-wrap img {
    position: absolute;
    width: 100%;
    transform: translate(-50%, -50%);
    left: 50%;
    top: 50%;
    z-index: 0;
}
.goods-item-icon-cart  .icon-loading {
    display: flex;
    width: 100%;
    height: 100%;
}
.goods-item-icon-cart .icon-loading img {
    width: 100%;
    transform: scale(1.3);
}

</style>
<template>
    <div class="goods-item">
        <div class="goods-image-wrap" @click='$emit("click", goodsItem)'>
            <img loading="lazy" :src="goodsItem.productImg">
        </div>
        <div class="goods-name">{{goodsItem.name}}</div>
        <div class="goods-price-wrap">
            <span class="sell-count">已售<span style="color: red">{{goodsItem.monthlyCount||0}}</span></span>
            <span class="goods-price">
                <span class="dp-price">¥{{goodsItem.dpPrice}}</span>
                <span class="sc-price" v-if="goodsItem.dpPrice !== goodsItem.scPrice">¥{{goodsItem.scPrice}}</span>
            </span>
            <span class="goods-item-icon-cart" @click='getProductDetail'>
                <span v-show="loadingDetail==false" class="icon-cart-loading"></span>
                <span class="icon-loading" v-show="loadingDetail == true">
                    <img src="../css/images/loading.svg">
                </span>
            </span>
        </div>
    </div>
</template>
<script>
    import { getProductDetail } from './api'
    export default {
        name: 'goods-item',
        props: {
            goodsItem: {
                required: true
            },
            loading: {
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
                loadingDetail: false
            }
        },
        methods: {
            getProductDetail(){
                if(this.loading) {
                    return;
                }
                
                this.$emit("update:loading", true);
                this.loadingDetail = true;
                getProductDetail({id: this.goodsItem.id})
                .then((data)=> {
                    this.$emit("update:loading", false);
                    this.loadingDetail = false;
                    this.$emit("goodsItemDetail", data);
                },(error)=> {
                    this.$emit("update:loading", false);
                    this.loadingDetail = false;
                    this.$toast("获取商品详情失败");
                }).catch(()=> {
                    this.$emit("update:loading", false);
                    this.loadingDetail = false;
                    this.$toast("获取商品详情失败");
                })
            }
        }
    }
</script>